﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace BLL.Action.Core.Macro
{
    public class TransfersMacro
    {
        public const string Year = "year";

        public const string Month = "month";

        public const string TwoDays = "twoday";

        public const string Today = "todays";

        public const string Yesterday = "yesterday";

        public const string Week = "week";

        public const string MinDate = "mindate";

        //public MacroHelper(string leftTag, string rightTag)
        //{
        //    string pattern = @"{0}(?<{1}>[^{2}]+){2}";
        //    pattern = string.Format(pattern,leftTag,MACRO_GROUP,rightTag);
        //    macroReg_ = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);

        //    leftTag_ = leftTag;
        //    rightTag_ = rightTag;
        //}
         //string pattern = @"{0}(?<{1}>[^{2}]+){2}";
            //pattern = string.Format(pattern,leftTag,MACRO_GROUP,rightTag);
            //macroReg_ = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);

       static  Regex reg = new Regex(@"#(?<Macro>[^#]+)#", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);

        public static string GetMacro(string Macro)
        {
            
            MatchCollection mCollection=reg.Matches(Macro);
            foreach (Match c in mCollection)
            {
                switch (c.Groups["Macro"].Value)
                {
                    case Year: Macro = reg.Replace(Macro, DateTime.Now.Year.ToString(), 1);
                        break;
                    case Month: Macro = reg.Replace(Macro, DateTime.Now.Month.ToString(), 1);
                     break;
                    case TwoDays: Macro = reg.Replace(Macro, DateTime.Now.AddDays(-2).ToShortDateString(), 1);
                     break;
                    case Week: Macro = reg.Replace(Macro, DateTime.Now.AddDays(-7).ToShortDateString(), 1);
                     break;
                    case MinDate: Macro = reg.Replace(Macro, "1900-01-01", 1);
                    break;
                    case Today: Macro = reg.Replace(Macro, DateTime.Now.ToShortDateString(), 1);
                     break;
                    case Yesterday: Macro = reg.Replace(Macro, DateTime.Now.AddDays(-1).ToShortDateString(), 1);
                     break;
                }
            }


            return Macro;
        }
    }
}
